//
//  YHYReSubEveryButtonViewController.m
//  TripTogether
//
//  Created by 杨泓晔 on 15/12/30.
//  Copyright © 2015年 杨泓晔. All rights reserved.
//

#import "YHYReSubEveryButtonViewController.h"
#import "YHYReSubEveryDayCollectionViewCell.h"
#import "WJQEverydayModel.h"


#import "YHYReSubDetailViewController.h"

@interface YHYReSubEveryButtonViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property (nonatomic,retain)UICollectionViewFlowLayout *flowlayout;
@property (nonatomic,retain) UICollectionView *collView;
@property (nonatomic,retain)NSMutableDictionary *myDic;
@property (nonatomic,assign)NSInteger page;
@property (nonatomic,assign)BOOL upload;


@property (nonatomic,strong)NSMutableArray *arrayModel;

@end

@implementation YHYReSubEveryButtonViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _arrayModel = [NSMutableArray array];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithRed:0.980 green:0.961 blue:0.910 alpha:1.000];
    _myDic = [NSMutableDictionary dictionaryWithObject:_arrayModel forKey:@"1"];
    [self createView];
    
   
    // 下拉刷新
    [self addHeaderRefresh];
    [self addFooterRefresh];

    self.navigationItem.title = @"精选故事";
    
    
}
- (void)addHeaderRefresh
{
    _collView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        NSLog(@"下拉");
        self.upload = NO;
        self.page = 0;
        [self getData:self.page];
    }];
    // 自动开始下拉刷新
    [_collView.mj_header beginRefreshing];
}
- (void)addFooterRefresh
{
    _collView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
       self.page++;
        self.upload = YES;
        NSLog(@"上拉");
        [self getData:self.page * 12];
    }];
}



- (void)getData:(NSInteger)page{
    
    
    
       [self MBShow];
   // NSString *str = @"http://api.breadtrip.com/v2/new_trip/spot/hot/list/?start=1";
    NSString *str = [NSString stringWithFormat:@"http://api.breadtrip.com/v2/new_trip/spot/hot/list/?start=%ld",page];
    [WJQNetwork getAFNet:str block:^(id result) {
        
        
     
        NSLog(@"%ld",page);
        
        if (self.upload == NO) {
            NSMutableArray *arr = [_myDic objectForKey:@"1"];
            [arr removeAllObjects];
        }
        
        
        
        NSMutableDictionary *dic = result;

        NSMutableDictionary *dicData = [dic objectForKey:@"data"];
        
        NSMutableArray *arrayHot_spot_list = [dicData objectForKey:@"hot_spot_list"];
        
     
        
        for (NSMutableDictionary *dicSpot in arrayHot_spot_list) {
            
//            YHYReSubEveryDayModel *model = [YHYReSubEveryDayModel modelWithDicationary:dicSpot];
            
            WJQEverydayModel *everyDayMod = [WJQEverydayModel modelWithDicationary:dicSpot];
            
            [everyDayMod setValuesForKeysWithDictionary:[dicSpot objectForKey:@"user"]];
    
            
//            [_arrayModel addObject:everyDayMod];
            
            NSMutableArray *arr = [_myDic objectForKey:@"1"];
            [arr addObject:everyDayMod];
        }
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        //存数组
        
  //      *** 第四步：结束上拉，下拉
        //***
        [_collView.mj_header endRefreshing];
        [_collView.mj_footer endRefreshing];


  
        [_collView reloadData];
        
        [self MBHide];


    } errorBlock:^(NSError *error) {
        
        [self NetworkError:^{
            
            
        }];
    }];
}


- (void)createView{
    _flowlayout = [UICollectionViewFlowLayout new];
    
    //..默认最小为10，如果比10小，需要重新付值
    _flowlayout.minimumInteritemSpacing = 10;
    //行
    _flowlayout.minimumLineSpacing = 10;
    
    //section内置大小
   // _flowlayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);

    
    //多种item大小，使用estimatedItemSize提高代码运算效率 提高流畅度
   // _flowlayout.estimatedItemSize = CGSizeMake((SCREEN_WIDTH - 30)/2, 230);

    
    
    _collView = [[UICollectionView alloc]initWithFrame:self.view.frame collectionViewLayout:_flowlayout];
    _collView.backgroundColor  = [UIColor colorWithRed:0.980 green:0.961 blue:0.910 alpha:1.000];
    _collView.delegate = self;
    _collView.dataSource = self;
    [self.view addSubview:_collView];
    
    [_collView registerClass:[YHYReSubEveryDayCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    
    

}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    
            
            return CGSizeMake((SCREEN_WIDTH - 30)/2, 230);
}

// 返回每个区的边界距离
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    
        return UIEdgeInsetsMake(10, 10, 10, 10);
}



- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    NSMutableArray *array  = [_myDic objectForKey:@"1"];
    return array.count;
    
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    YHYReSubEveryDayCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    cell.myModel = [[_myDic objectForKey:@"1"] objectAtIndex:indexPath.item];
     return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    YHYReSubDetailViewController *yhyVC = [YHYReSubDetailViewController new];
    
    WJQEverydayModel *userModel = [[_myDic objectForKey:@"1"] objectAtIndex:indexPath.item];

    yhyVC.navigationItem.title = @"故事详情";
    
    

    yhyVC.everydayModel = userModel;
    
 
    
      
      
    
    
    
    [NSNumber numberWithFloat:[userModel.spot_id floatValue]];
    [self.navigationController pushViewController:yhyVC animated:yhyVC];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
